import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Screen{
	static Player p1 = new Player(1000, 230);
	static Player p2 = new Player(25, 230);
	static Ball b1 = new Ball(520,275);
	//520 X, 266 Y
	public Screen(){
		
		Screen.p1.addKeyListener(new KL());
	}
	private Font font = new Font("Cambria", Font.PLAIN, 123);
	static int cScore = 0;
	static int pScore = 0;
	public void paintComponent(Graphics g){
		Graphics2D po = (Graphics2D)g;
		po.setColor(Color.black);
		po.fillRect(0, 0, Game.getWIDTH(), Game.getHEIGHT());
		for(int i = 0; i < 14; i++){
			po.setColor(Color.white);
			po.fillRect(520, 47*i, 10, 20);
		}
		po.setFont(font);
		po.drawString(Integer.toString(cScore), 300, 100);
		po.drawString(Integer.toString(pScore), 700, 100);
		p1.paintComponent(g);
		p2.paintComponent(g);
		if (Game.winner == 0){
			b1.paintComponent(g);
		}
		if (Game.winner == Game.LEFT){
			drawWL(g);
		}else if (Game.winner == Game.RIGHT){
			drawLW(g);
		}
		//p1.y+=2;
	}

	
	public static void update(){ 
		if (Game.winner == 0){
			Screen.p1.update();
			Screen.p2.updateC();
			Screen.b1.update();
		}
		esqFromRight();
		esqFromLeft();
	}
	private Font font2 = new Font("Cambria", Font.BOLD, 45);
	public void drawWL(Graphics g){
		Graphics2D po = (Graphics2D) g;
		po.setFont(font2);
		po.drawString("YOU \n WIN",200,180);
		po.drawString("YOU \n LOSE",650,180);
		po.setColor(Color.red);
		po.drawString("Press <R> to restart!", 325, 350);
	}
	public void drawLW(Graphics g){
		Graphics2D po = (Graphics2D) g;
		po.setFont(font2);
		po.drawString("YOU \n LOSE",200,180);
		po.drawString("YOU \n WIN",650,180);
		po.setColor(Color.red);
		po.drawString("Press <R> to restart!", 325, 350);
	}
	public static void esqFromRight(){
		if (b1.x > Game.getWIDTH()){
			cScore +=1;
			if(cScore == 7){
				
				Game.winner = Game.LEFT;
				b1 = new Ball(520,275);
			}else{
				resetThem();
			}
				
		}
	}
	public static void esqFromLeft(){
		if (b1.x < 0){
			pScore +=1;
			if(pScore == 7){
				Game.winner = Game.RIGHT;
				b1 = new Ball(520,275);
			}else if(pScore != 10){
				resetThem();
			}
		}
	}
	public static void resetThem(){
		b1 = new Ball(520,275);
	}
	
	
}

class KL implements KeyListener{

	boolean wPressed = false;
	boolean sPressed = false;
	@Override
	public void keyPressed(KeyEvent e) {
		
		if(e.getKeyCode() == KeyEvent.VK_W){
			Screen.p1.moveUp();
			wPressed = true;
			
		}else if(e.getKeyCode() == KeyEvent.VK_S){
			sPressed = true;
			Screen.p1.moveDown();	
		}
		
		
	}

	@Override
	public void keyReleased(KeyEvent ev) {
		if(ev.getKeyCode() == KeyEvent.VK_W){
			wPressed = false;
			
		}else if(ev.getKeyCode() == KeyEvent.VK_S){
			sPressed = false;
		}
		if(wPressed == false && sPressed == false){
		Screen.p1.backNormal2();
		}
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
}